<!DOCTYPE html>
<html>
  
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  <meta name="author" content="Amos Zhu">
  
  
  <title>SpringBoot+Redis+MemCache+Nginx+Lua实现三级缓存架构（二）——Nginx环境安装和整合Lua | Amos的技术博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="stuff,redis,spring boot,nginx,memcache,">
  

  
  <meta name="description" content="前面我们已经简单的介绍过了三级缓存的概念，接下来的博文中，我们需要搭建系统运行的环境、编写功能实现代码来实现我们的三级缓存架构，工欲善其事必先利其器，本片博文我们先来搭建Nginx环境以及整合Lua">

  

  
    <script src="//cdn.jsdelivr.net/npm/leancloud-storage@3.11.1/dist/av-min.js" async></script>
  

  
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
  

  
    <script src="//unpkg.com/valine/dist/Valine.min.js" async></script>
  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"eObxCkTJ6j4GzRxGLgiHyfJa-gzGzoHsz","appkey":"fzRMAFC6MCE11mp7mnvh4Kwe","comment":true,"count":true},
    welcome: {"enable":true,"interval":30},
    start_time: "2019-05-28",
    passwords: ["efe07af7441da2b69c4a41e42e73be4db47f66010a56900788a458354a7373ec", ],
    is_post: true,
    lock: false,
    author: "Amos Zhu",
    share: {"qq":true,"wechat":true},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  <script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>

  
    <link rel="icon" href="/blog/images/favicon.ico">
    <link rel="apple-touch-icon" href="/blog/images/touch-icon.png">
  

  <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

  <link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">

  
</head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">Amos Zhu</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 当浮躁的心静不下，不妨写些东西或者看会书</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">首页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/redis/" target="_self">redis</a>
      
        <a href="/blog/rabbitMQ/" target="_self">rabbitMQ</a>
      
        <a href="/blog/elasticsearch" target="_self">elasticsearch</a>
      
        <a href="/blog/stuff" target="_self">stuff</a>
      
        <a href="/blog/kafka" target="_self">kafka</a>
      
        <a href="/blog/friends/" target="_self">友链</a>
      
        <a href="/blog/about/" target="_self">关于</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://gitee.com/amos_zhu" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            首页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/redis/" target="_self">
            redis
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/rabbitMQ/" target="_self">
            rabbitMQ
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/elasticsearch" target="_self">
            elasticsearch
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/stuff" target="_self">
            stuff
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/kafka" target="_self">
            kafka
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/friends/" target="_self">
            友链
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/about/" target="_self">
            关于
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2019-08-26
    </span>
    
      <span>
        | <a href="/blog/categories/stuff/"><i class="fa fa-bookmark"></i>stuff</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
  </div>
  <h1 class="passage-title">
    SpringBoot+Redis+MemCache+Nginx+Lua实现三级缓存架构（二）——Nginx环境安装和整合Lua
  </h1>
  
  <article class="passage-article">
    <br>Redis的环境搭建我们之前的博文 <a href="https://www.jianshu.com/p/a0bd76adcc1d" target="_blank" rel="noopener">《Redis教程（一）——Redis安装》</a> 已经搭建好了，本文只是实现三级缓存架构，所以Redis这里我们这暂时不做分布式集群，只使用单机版即可，以后如果有需要将SpringBoot整合Redis的方便变更下即可，不需要做代码层面上的改动。<p></p>

<p>Redis环境我们准备好了，现在我们开始在Centos7中搭建Nginx和Lua整合，这里我们使用流行的开源方案——OpenResty</p>
<p>为什么选择<code>OpenResty</code>呢，OpenResty中集成了Nginx+Lua环境，并且还提供了Redis客户端、mysql客户端，http客户端等大量的组件，方便我们搭建环境和开发，更方便的是，这个是我们中国人发明的，官方文档也是中文的，所以很方便我们查找相关的资料，对于英文不是很好的同学，真是极大的帮助了。</p>
<p>安装的方法，可以参考<a href="http://openresty.org/cn/installation.html" target="_blank" rel="noopener">OpenResty-安装</a>， 如果不想看官网的话，也可以阅读下面的安装方法，跟官网上的差不多</p>
<h3 id="安装环境校验"><a href="#安装环境校验" class="headerlink" title="安装环境校验"></a>安装环境校验</h3><p>首先查看Centos7是否安装了一下的软甲<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 查看是否存在gcc, Nginx依赖该环境</span></span><br><span class="line">rpm -qa|grep gcc</span><br><span class="line"></span><br><span class="line"><span class="comment">## 查看是否安装 Perl,nginx 的 http 模块使用 pcre 来解析正则表达式，所以需要在 linux 上安装 pcre 库，pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库</span></span><br><span class="line">rpm -qa|grep pcre</span><br><span class="line">rpm -qa|grep pcre-devel</span><br><span class="line"></span><br><span class="line"><span class="comment">## 查看是否安装curl</span></span><br><span class="line">rpm -qa|grep curl</span><br><span class="line"></span><br><span class="line"><span class="comment">## 查看是否安装 openssl</span></span><br><span class="line">rpm -qa|grep openssl</span><br><span class="line">rpm -qa|grep openssl-devel</span><br></pre></td></tr></table></figure></p>
<p>上面那个软件没有安装，则需要安装，已安装的可以不必再安装<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install pcre-devel openssl-devel gcc curl</span><br></pre></td></tr></table></figure></p>
<h3 id="安装-OpenResty"><a href="#安装-OpenResty" class="headerlink" title="安装 OpenResty"></a>安装 OpenResty</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 添加 openresty分库,便于安装或者更新软件包</span></span><br><span class="line">yum install yum-utils</span><br><span class="line">yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo</span><br><span class="line"></span><br><span class="line"><span class="comment">## 安装openresty 和 resty命令行工具</span></span><br><span class="line">yum install openresty</span><br><span class="line">yum install openresty-resty</span><br><span class="line"></span><br><span class="line"><span class="comment">## 查看可安装的组件（当前可不安装，后期想要安装的时候，再查看具体的安装名称</span></span><br><span class="line">yum --disablerepo=<span class="string">"*"</span> --enablerepo=<span class="string">"openresty"</span> list available</span><br></pre></td></tr></table></figure>
<p>至此我们已经安装好<code>penResty</code>的环境了，接下来我们先看看怎么使用<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 创建nginx运行需要的目录环境</span></span><br><span class="line">mkdir work</span><br><span class="line"><span class="built_in">cd</span> work</span><br><span class="line">mkdir logs/ conf/</span><br><span class="line"></span><br><span class="line"><span class="comment">##创建nginx.conf文件</span></span><br><span class="line"><span class="built_in">cd</span> conf</span><br><span class="line">vi nginx.conf</span><br></pre></td></tr></table></figure></p>
<p>上述需要创建 nginx.conf文件，并且输入一下内容<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">## nginx.conf</span><br><span class="line">worker_processes  1;</span><br><span class="line">error_log logs/error.log;</span><br><span class="line">events &#123;</span><br><span class="line">    worker_connections 1024;</span><br><span class="line">&#125;</span><br><span class="line">http &#123;</span><br><span class="line">    server &#123;</span><br><span class="line">        listen 8080;</span><br><span class="line">        location / &#123;</span><br><span class="line">            default_type text/html;</span><br><span class="line">            content_by_lua_block &#123;</span><br><span class="line">                ngx.say(&quot;&lt;p&gt;hello, world&lt;/p&gt;&quot;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>配置Nginx的环境变量<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 编辑 /etc/profile 文件，配置好nginx的环境变量</span></span><br><span class="line">vi /etc/profile</span><br><span class="line"></span><br><span class="line">......</span><br><span class="line">NGINX_HOME=/usr/<span class="built_in">local</span>/openresty/nginx</span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:<span class="variable">$NGINX_HOME</span>/sbin</span><br><span class="line">......</span><br><span class="line"></span><br><span class="line"><span class="comment">## 环境变量生效</span></span><br><span class="line"><span class="built_in">source</span> /etc/profile</span><br></pre></td></tr></table></figure></p>
<p>查看环境变量配置是否生效<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 如果执行该命令 提示说查找不道nginx命令，则说明你的环境变量配置的 有问题，需要仔细查看下配置</span></span><br><span class="line">nginx -v</span><br></pre></td></tr></table></figure></p>
<p>启动Nginx环境<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 启动Nginx环境 该命令必须在你刚才创建的work目录下执行，否则启动失败</span></span><br><span class="line">nginx -p `<span class="built_in">pwd</span>`/ -c conf/nginx.conf</span><br></pre></td></tr></table></figure></p>
<p>访问 <a href="http://192.168.56.105:8080/" target="_blank" rel="noopener">http://192.168.56.105:8080/</a> 看看浏览器是不是显示 Hello world ，如果是则说明Nginx搭建成功</p>
<h3 id="整合Lua"><a href="#整合Lua" class="headerlink" title="整合Lua"></a>整合Lua</h3><p>在整合之前我们先看下<code>OpenResty</code>目录下是否存在 <code>lualib</code>文件夹，<code>OpenResty</code>默认的安装路径是<code>/usr/localhost/openresty</code>这里面是lua的依赖，我们编写脚本时，需要手动引入这些依赖</p>
<p><img src="https://s2.ax1x.com/2019/08/26/mWwcRI.png" alt="image"></p>
<p>首先先定义我们的工程目录结构：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">--work</span><br><span class="line">----conf</span><br><span class="line">------nginx.conf</span><br><span class="line">----lua</span><br><span class="line">------requestDirect.conf</span><br><span class="line">----lualib</span><br><span class="line">------*.lua</span><br><span class="line">------*.so</span><br></pre></td></tr></table></figure></p>
<p>接下来我们需要在我们之前<code>/work/conf/nginx.conf</code>中的http模块中添加lua的依赖</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- nginx.conf</span></span><br><span class="line"></span><br><span class="line">worker_processes  <span class="number">1</span>;</span><br><span class="line">error_log logs/<span class="built_in">error</span>.<span class="built_in">log</span>;</span><br><span class="line">events &#123;</span><br><span class="line">    worker_connections <span class="number">1024</span>;</span><br><span class="line">&#125;</span><br><span class="line">http &#123;</span><br><span class="line">   lua_package_path <span class="string">"/usr/local/openresty/lualib/?.lua;;"</span>;</span><br><span class="line">   lua_package_cpath <span class="string">"/usr/local/openresty/lualib/?.so;;"</span>;</span><br><span class="line">   include lua/requestDirect.conf;</span><br><span class="line">   server &#123;</span><br><span class="line">      listen <span class="number">8080</span>;</span><br><span class="line">      location / &#123;</span><br><span class="line">         default_type text/html;</span><br><span class="line">         content_by_lua_block &#123;</span><br><span class="line">             ngx.say(<span class="string">"&lt;p&gt;hello, world&lt;/p&gt;"</span>)</span><br><span class="line">         &#125;</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>requestDirect.conf</code>文件的内容</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- requestDirect.conf</span></span><br><span class="line">server &#123;</span><br><span class="line">    listen     <span class="number">8080</span>;</span><br><span class="line">    server_name _;</span><br><span class="line">    location /lua &#123;  </span><br><span class="line">      default_type <span class="string">'text/html'</span>;  </span><br><span class="line">      content_by_lua_block &#123;</span><br><span class="line">             ngx.say(<span class="string">"&lt;p&gt;hello, world&lt;/p&gt;"</span>)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时文件就添加完成了，先来测试下是否能启动成功<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 进入到 /home/work 目录下</span></span><br><span class="line"><span class="built_in">cd</span> /home/work</span><br><span class="line"></span><br><span class="line"><span class="comment">## 编译 看看能否编译通过</span></span><br><span class="line">nginx -t</span><br><span class="line"></span><br><span class="line"><span class="comment">## 查看端口是否被占用</span></span><br><span class="line">losf -i:8080</span><br><span class="line"><span class="comment">## 删除进程 pid 此处的pid是lsof命令查询出来的，有几个就执行几次</span></span><br><span class="line"><span class="built_in">kill</span> -9 pid</span><br><span class="line"></span><br><span class="line"><span class="comment">## 启动nginx服务</span></span><br><span class="line">nginx -p `<span class="built_in">pwd</span>`/ -c /home/work/conf/nginx.conf</span><br></pre></td></tr></table></figure></p>
<p>启动完成之后，访问 <code>http://192.168.56.105:8080/lua</code> 就能看到返回的结果了</p>
<h3 id="项目工程文件"><a href="#项目工程文件" class="headerlink" title="项目工程文件"></a>项目工程文件</h3><p>上面是我们测试Nginx是否集成Lua成功的，现在我们将项目工程话，目录结构依旧跟上面是一样的，我们将业务代码独立出来，不跟系统相关的代码放在一起。</p>
<p>上面还有一点，我们每次更新都需要手动kill掉nginx的进程，很麻烦，我们可以使用nginx的 reload方式来重新加载lua的配置</p>
<p>目录结构如下：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">--work</span><br><span class="line">----conf</span><br><span class="line">------nginx.conf</span><br><span class="line">----lua</span><br><span class="line">------requestDirect.lua</span><br><span class="line">--lualib</span><br><span class="line">----*.lua</span><br><span class="line">----*.so</span><br></pre></td></tr></table></figure></p>
<ul>
<li>按照上述的工程目录结构,我们创建项目需要的文件夹<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## /home/work下创建文件 conf文件夹 lua文件夹</span></span><br><span class="line"><span class="built_in">cd</span> /home/work</span><br><span class="line">mkdir conf</span><br><span class="line">mkdir lua</span><br><span class="line"></span><br><span class="line"><span class="comment">## 创建 nginx.conf 和  lua/requestDirect.lua</span></span><br><span class="line">vi nginx.conf</span><br><span class="line">vi lua/requestDirect.lua</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>在<code>/home/work/conf/nginx.conf</code>中添加如下的代码<br><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 注意这里的路径必须是绝对路径才行，如果是相对路径，则会直接请求到nginx的安装路径下，请求时报找不到对应文件的错误</span></span><br><span class="line">  server &#123;</span><br><span class="line">    listen <span class="number">80</span>;</span><br><span class="line">    location /lua &#123;</span><br><span class="line">       default_type text/html;</span><br><span class="line">    content_by_lua_file /home/work/conf/lua/requestDirect.lua;</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure></p>
<p><code>/home/work/conf/lua/requestDirect.lua</code>文件中添加如下的代码<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ngx.say(<span class="string">"hello world,this is nginx_lua project file"</span>);</span><br></pre></td></tr></table></figure></p>
<p>将nginx目录下的 lualib依赖拷贝到 <code>/home/work/lualib</code>中<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 在/home/work中拷贝 nginx目录下的lualib依赖文件</span></span><br><span class="line"><span class="built_in">cd</span> /home/work</span><br><span class="line">cp -r /usr/<span class="built_in">local</span>/openresty/lualib/ .</span><br></pre></td></tr></table></figure></p>
<ul>
<li>我们需要更新nginx源目录下的conf/nginx.conf文件，并且在其中引入我们的工程目录的conf文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 进入到nginx的根目录下</span></span><br><span class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/openresty/nginx/</span><br><span class="line"></span><br><span class="line">vi conf/nginx.conf</span><br><span class="line"><span class="comment">## 在nginx.conf 中的http模块中添加</span></span><br><span class="line"></span><br><span class="line">....</span><br><span class="line">lua_package_path <span class="string">"/home/work/lualib/?.lua;;"</span>;</span><br><span class="line">lua_package_cpath <span class="string">"/home/work/lualib/?.so;;"</span>;</span><br><span class="line">include /home/work/conf/nginx.conf</span><br><span class="line">....</span><br></pre></td></tr></table></figure>
<ul>
<li>上面的文件添加完成之后我们就可以使用nginx本身自带的命令来启动nginx了<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 先查看能否编译成功，如果编译不通过会提示具体的错误</span></span><br><span class="line">nginx -t</span><br><span class="line"></span><br><span class="line"><span class="comment">## 重新加载</span></span><br><span class="line">nginx -s reload</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>至此我们的nginx整合lua在项目工程中配置的Demo示例已经完成了，在浏览器中访问 <code>http://192.168.56.105:8080/lua</code> 就可以看到打印的信息了</p>
<p>下篇博文我们将在此基础上，完成请求定向分发的功能</p>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#安装环境校验"><span class="toc-text">安装环境校验</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#安装-OpenResty"><span class="toc-text">安装 OpenResty</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#整合Lua"><span class="toc-text">整合Lua</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#项目工程文件"><span class="toc-text">项目工程文件</span></a></li></ol>
  </div>
</aside>
  
    <aside class="passage-copyright">
      <div>本文作者: Amos Zhu</div>
      
        <div>
          原文链接: 
          <a href target="_blank">http://amos_zhu.gitee.io/passages/SpringBoot-Redis-MemCache-Nginx-Lua实现三级缓存架构（二）——Nginx环境安装和整合Lua/</a>
        </div>
      
      <div>
        版权声明: 本博客所有文章除特别声明外, 均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议. 转载请注明出处!
      </div>
    </aside>
  
  
    <div class="passage-tags">
     
      <a href="/blog/tags/redis/"><i class="fa fa-tags"></i>redis</a>
     
      <a href="/blog/tags/spring-boot/"><i class="fa fa-tags"></i>spring boot</a>
     
      <a href="/blog/tags/nginx/"><i class="fa fa-tags"></i>nginx</a>
     
      <a href="/blog/tags/memcache/"><i class="fa fa-tags"></i>memcache</a>
    
    </div>
  
</div>

    </main>
    
      
<div class="site-comment-contanier" data-plateform="leancloud">
  
    <p id="site-comment-info">
      <i class="fa fa-spinner fa-spin"></i> 评论加载中
    </p>
    <div id="site-comment"></div>
  
</div>
    
    <div class="site-footer-wrapper">
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">博客推荐</h5>
          
            <span class="site-footer-item">
              <a href="http://youzhixueyuan.com/series/java" target="_blank">优知学院</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://ruanyifeng.com/" target="_blank">阮一峰的个人网站</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">系列教程</h5>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/redis" target="_blank">Redis系列教程</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/rabbitMQ/" target="_blank">RabbitMQ教程</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/kafka/" target="_blank">Kafka架构探险</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">抓到我</h5>
          
            <span class="site-footer-item">
              <a href="https://gitee.com/amos_zhu" target="_blank">Gitee</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://blog.csdn.net/wdcl2468" target="_blank">CSDN</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://www.jianshu.com/u/2dbe61b1f3c3" target="_blank">简书</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: amoszhu@aliyun.com
      </div>
    
    <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div>
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <div class="site-layer-input-choose">
          <a href="javascript:void(0);" title="Change Search Engine">Google</a>
        </div>
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">
  <div class="bottom-bar-left">
    <a href="/blog/passages/SpringBoot-Redis-MemCache-Nginx-Lua实现三级缓存架构（一）——三级缓存架构体系/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/passages/缓存与数据库双写一致性的解决方案——附上代码解决方案/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
      <a href="javascript:void(0);" id="site-reward">
        <i class="fa fa-thumbs-up"></i>
      </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
    <a id="share-btn-wechat" href="javascript:void(0);" target="_blank">
      <i class="fa fa-wechat"></i>
    </a>
  
</div>
    


  <script async>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>



  <script async>
    (function(){
    var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?":"https://jspassport.ssl.qhimg.com/11.0.1.js?";
    document.write('<script src="' + src + '" id="sozz"><\/script>');
    })();
  </script>


    
  </body>
</html>